package com.primal.config;

import com.primal.ws.endpoint.PrimalService;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.apache.cxf.bus.spring.SpringBus;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxws.EndpointImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.jws.WebService;
import javax.xml.ws.Endpoint;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Author whh
 * Date 2023/12/07/ 22:39
 * <p></p>
 */
@Configuration
@ImportResource(value = {"classpath*:META-INF/cxf/cxf.xml"})
public class WebServiceConfig {

    @Autowired
    private SpringBus bus;

    @Autowired
    private PrimalService service;
    @Bean
    public Endpoint endpoint() {
        EndpointImpl endpoint = new EndpointImpl(bus,service);
        endpoint.getInInterceptors().add(new LoggingInInterceptor());
        endpoint.getOutInterceptors().add(new LoggingOutInterceptor());

        //将serviceName作为线程池前缀
        WebService annotation = service.getClass().getAnnotation(WebService.class);
        String prefix = annotation.serviceName();
        ThreadPoolExecutor executor = new ThreadPoolExecutor(
                10,
                50,
                2L,
                TimeUnit.SECONDS,
                new LinkedBlockingQueue<>(),
                new BasicThreadFactory.Builder().namingPattern(prefix+"-thread-pool-%d").daemon(true).build(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        //设置线程池
        endpoint.setExecutor(executor);
        endpoint.publish("/api");
        return endpoint;
    }
}
